package com.lin.linchatgpt.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.lin.linchatgpt.entity.Image;
import com.lin.linchatgpt.entity.Message;
import com.lin.linchatgpt.entity.vo.ImageMessageVo;
import com.lin.linchatgpt.mapper.ImageMapper;
import com.lin.linchatgpt.service.ImageService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Copyright(C),2022年-2022,霖霖
 *
 * @author 霖霖
 * @version 1.0
 * @date 2023/7/8 17:49
 * @Description
 */
@Service
@Slf4j
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements ImageService {
    @Resource
    private ImageMapper imageMapper;

    @Override
    public List<ImageMessageVo> getImageChat() {
        String loginId = (String) StpUtil.getLoginId();

        List<Image> list = imageMapper.getImages(loginId);

        ArrayList<ImageMessageVo> imageMessageVos = new ArrayList<>();

        for (Image image : list) {
            Message question = new Message();

            question.setId(image.getId());
            question.setContent(image.getPrompt());


            ImageMessageVo chatMessageVo = new ImageMessageVo(question, image);
            imageMessageVos.add(chatMessageVo);
        }
        return imageMessageVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean mySave(Image image) {
        image.setId(UUID.randomUUID().toString().replace("-", ""));
        image.setIsDelete(0);
        return imageMapper.myInsert(image) > 0;
    }

}
